/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BussinessObject;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author ngoctoan
 */
public class GeneralBO {

    public static void HienRSLenTB(ResultSet rs, JTable tb) {
        tb.removeAll();
        DefaultTableModel model = new DefaultTableModel();
        try {
            ResultSetMetaData metaPhongBan = rs.getMetaData();

            //            String cols[] = {"Cột 1", "Cột 2", "Cột 3", "Cột 4"};
            String cols[] = new String[metaPhongBan.getColumnCount()];
            for (int i = 0; i < cols.length; i++) {
                cols[i] = metaPhongBan.getColumnLabel(i + 1);
            }
            model.setColumnIdentifiers(cols);

            while (rs.next()) {
                Object dataRow[] = new Object[cols.length];
                for (int i = 0; i < dataRow.length; i++) {
                    dataRow[i] = rs.getObject(i + 1);
                }
                model.addRow(dataRow);
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        tb.setModel(model);
    }
    
    public static void HienRSLenCombobox (ResultSet rs, JComboBox cb){
        cb.removeAll();
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        try {
            //model.setColumnIdentifiers(cols);
            //rs.next();
            while (rs.next()) {
                model.addElement(rs.getObject(1));
                System.out.println(rs.getObject(1));
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        cb.setModel(model);
        
    }
    
     public static int getNumRowRS(ResultSet rs) throws SQLException {
        int row = 0;
        while (rs.next()) {
            row++;
        }
        return row;
    }
}